package com.hxkj.service.impl;


import com.hxkj.client.service.NoticeServiceClient;
import com.hxkj.domain.enums.NoticeExceptionEnum;
import com.hxkj.domain.po.Notice;
import com.hxkj.service.NoticeService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 公告Service实现类
 * <p>
 * Created by chenfangzhou on 2018/6/6.
 */
@Service
@Slf4j
public class NoticeServiceImpl implements NoticeService {


    @Autowired
    private NoticeServiceClient noticeServiceClient;


    @Override
    public ResultMessageStruct saveNotice(Notice notice) throws Exception {
        if (notice == null) {
            throw new CustomException(NoticeExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            // 查询站点列表
            result = noticeServiceClient.saveNotice(notice);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(NoticeExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(NoticeExceptionEnum.getEnumByCode(result.getCode()));
        }
    }


    @Override
    public ResultPageStruct<Notice> getNoticeList(Notice notice, PageTo pageParam) throws Exception {
        ResultPageStruct<Notice> result;
        ParameterDataStruct<Notice> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(notice);
        parameterDataStruct.setPageTo(pageParam);
        try {
            // 查询站点列表
            result = noticeServiceClient.getNoticeList(parameterDataStruct);
            result.setCode(null);
            result.setMessage(null);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(NoticeExceptionEnum.NOTICE_QUERY_ERROR);

        }

    }

    @Override
    public ResultDataStruct<Notice> getNotice(Notice notice) throws Exception {
        if (notice == null) {
            throw new CustomException(NoticeExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultDataStruct<Notice> result = new ResultDataStruct();
        try {
            result = noticeServiceClient.getNotice(notice);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(NoticeExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(NoticeExceptionEnum.getEnumByCode(result.getCode()));
        }
    }

    @Override
    public ResultMessageStruct deleteNotice(Notice notice) throws Exception {
        if (notice == null) {
            throw new CustomException(NoticeExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            result = noticeServiceClient.deleteNotice(notice);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(NoticeExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(NoticeExceptionEnum.getEnumByCode(result.getCode()));
        }
    }

    @Override
    public ResultMessageStruct updateNotice(Notice notice) throws Exception {
        if (notice == null) {
            throw new CustomException(NoticeExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            result = noticeServiceClient.updateNotice(notice);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(NoticeExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(NoticeExceptionEnum.getEnumByCode(result.getCode()));
        }
    }


}
